package com.anosine.gpsl.android.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.view.WindowManager;

/**
 * @Title: Loading
 * @Package com.anosine.gpsl.android.base
 * @Description: Loading
 * @author: sunny
 * @date: 2015-11-18
 * @version V1.0
 */
public class Loading {
	private ProgressDialog mDialog;
	private Context mContext;
	private int mCountShow;

	/**
	 * @Title: Loading(Context context)
	 * @Description: 构造函数，初始化
	 * @param: context 上下文
	 * @throws
	 */

	public Loading(Context context) {
		mContext = context;
	}

	/**
	 * @Title: show()
	 * @Description: 显示等待窗
	 * @param: 无
	 * @return: 无
	 * @throws
	 */

	public void show() {
		show(null);
	}

	/**
	 * @Title: show(String title)
	 * @Description: 显示等待窗
	 * @param: title　显示内容
	 * @return: 无
	 * @throws
	 */

	public void show(String title) {
		mCountShow++;
		if (null == mDialog) {
			initDialog();
		}
		title = title == null || "".equals(title) ? "正在加载..." : title;
		mDialog.setMessage(title);
		mDialog.show();
	}

	/**
	 * @Title: dismiss()
	 * @Description: 取消等待窗
	 * @param: 无
	 * @return: 无
	 * @throws
	 */

	public void dismiss() {
		mCountShow--;
		if (0 >= mCountShow && null != mDialog) {
			mDialog.cancel();
			mCountShow = 0;
		}

		if (0 > mCountShow) {
			mCountShow = 0;
		}
	}

	/**
	 * @Title: initDialog()
	 * @Description: 初始化等待窗
	 * @param: 无
	 * @return: 无
	 * @throws
	 */

	private void initDialog() {
		mDialog = new ProgressDialog(mContext);
		mDialog.setCancelable(true);
		WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();	
		mDialog.getWindow().setAttributes(params);
	}

	/**
	 * @Title: dip2px(Context context, float dpValue)
	 * @Description: dip转换成px（像素）　单位转换是为了支持多分辨率手机的
	 * @param: context 上下文
	 * @param: dpValue　值
	 * @return: int
	 * @throws
	 */

	public int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

}
